package com.letere;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.letere.bean.*;
import com.letere.bean.Package;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.annotation.PostConstruct;

/**
 * springboot运行程序
 * @author letere
 * @create 2021-08-23 14:09
 */
@SpringBootApplication
public class Run {

    public static void main(String[] args) {
        SpringApplication.run(Run.class, args);
    }

// -----------------------------------------------------------
    @Autowired
    private Base base;

    @Autowired
    private DataBase dataBase;

    @Autowired
    private Package packageName;

    @Autowired
    private Other other;

    /**
     * 代码生成
     */
    @PostConstruct
    public void codeGenerate() {
        //1. 全局配置（com.baomidou.mybatisplus.generator.config.GlobalConfig）
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setActiveRecord(other.isAR()) //实体类开启AR模式
                .setAuthor(base.getCreator()) //作者信息
                .setOutputDir(base.getLocation()) //文件生成路径
                .setFileOverride(true) //文件覆盖
                .setIdType(IdType.AUTO) //主键类型
                .setServiceName("%sService") //设置Service名字，默认开头会多出一个I字母
                .setBaseResultMap(other.isResultMap()) //生成基本的结果返回集
                .setBaseColumnList(other.isBaseColumn()); //生成字段列表

        //2. 数据源配置
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL) //数据库类型
                .setDriverName(dataBase.getDriver())
                .setUsername(dataBase.getUsername())
                .setPassword(dataBase.getPassword())
                .setUrl(dataBase.getUrl());

        //3. 策略配置
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setCapitalMode(true) //全局大写命名
                .setEntityLombokModel(other.isLombok()) //Lombok实体类
                .setNaming(NamingStrategy.underline_to_camel) //驼峰命名
                .setEntityTableFieldAnnotationEnable(true) //使用@TableFiled注解
                .setRestControllerStyle(true); //使用@RestController注解

        //4. 包名配置
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent(packageName.getParent()) //父包位置
                .setMapper(packageName.getMapper()) //Mapper文件包名
                .setXml(packageName.getXml()) //xml文件包名
                .setService(packageName.getService()) //service文件包名
                .setController(packageName.getController()) //controller文件包名
                .setEntity(packageName.getEntity()); //entity文件包名

        //5. 整合配置
        AutoGenerator autoGenerator = new AutoGenerator();
        autoGenerator.setGlobalConfig(globalConfig)
                .setDataSource(dataSourceConfig)
                .setStrategy(strategyConfig)
                .setPackageInfo(packageConfig);

        //6. 代码生成
        autoGenerator.execute();
    }
}
